package blog.controller;

import blog.utils.DownloadUtils;
import cn.hutool.core.io.IoUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author qipan
 * @date 2019/11/29 9:37
 */
@WebServlet("/file/down")
public class DownloadController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    /*    // 文件路径
        String filename = req.getParameter("filename");
        String picFolder = "/file/";

        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(100, 20, 4, 20);
        captcha.write("/file/code.jpg");

        if (filename != null && filename.equals("")) {
            String mimeType = "image/gif";
            // 设置content类型
            resp.setContentType(mimeType);
            // 设置大小
            File file = new File(picFolder + filename);
            resp.setContentLength((int) file.length());
            // 打开文件并输出
            FileInputStream inputStream = new FileInputStream(file);
            OutputStream servletOutputStream = resp.getOutputStream();
            // 把文件复制到输出流
            byte[] data = new byte[1024];
            int count = 0;
            while ((count = inputStream.read(data)) >= 0) {
                servletOutputStream.write(data,0,count);
            }
            inputStream.close();
            servletOutputStream.close();
        }*/

//        ServletContext sc = getServletContext();
//
//        String mimeType = sc.getMimeType("/file/" + filename);
//
//        resp.setHeader("content-type",mimeType);
//
//        String agent = req.getHeader("user-agent");
          down(req, resp);
//        demo(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void down(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入down()");
        String filename = req.getParameter("filename");
        System.out.println(filename);
        ServletContext sc = getServletContext();
        String mimeType = sc.getMimeType(filename);
        System.out.println(mimeType);
        resp.setHeader("content-type", mimeType);
        String agent = req.getHeader("user-agent");
        resp.setHeader("content-disposition","attachment;filename=" + DownloadUtils.getName(agent,filename));
        InputStream resource = sc.getResourceAsStream("/file/" + filename);
        ServletOutputStream outputStream = resp.getOutputStream();
        IoUtil.copy(resource,outputStream);
        resource.close();
        outputStream.close();
    }

    private void demo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String filename = req.getParameter("filename");

        ServletContext sc = getServletContext();
        System.out.println("-------------");
        System.out.println("ServletContext:");
        System.out.println(sc);
        System.out.println("getInitParameter: " + sc.getInitParameter("encoding"));
        System.out.println("getContextPath: " + sc.getContextPath());
        String mineType = sc.getMimeType("1.doc");
        System.out.println("getMimeType:" + mineType);
        System.out.println("-------------");
        System.out.println("user-agent:");
        String agent = req.getHeader("user-agent");
        System.out.println(agent);
        System.out.println("-------------");
        System.out.println("content-type:");
        System.out.println(req.getHeader("content-type"));
        System.out.println("-------------");
    }
}
